<template>
  <div class="app-container home" v-if="userType==='00'">
    <el-row>
      <el-select v-model="channel" placeholder="请选择" style="width: 250px" @change="generateQRCode">
        <el-option v-for="dict in dict.type.channel" :key="dict.value" :label="dict.label" :value="dict.value" />
      </el-select>
    </el-row>


    <el-row style="border: 1px solid; width: 255px">
      <div style="text-align: center;position: relative;top:10px">{{ channel }}</div>
      <canvas ref="qrCanvas" id="QRCode_header"></canvas>
    </el-row>

  </div>

  <div v-else class="app-container home">
  </div>
</template>

<script>
import QRCode from "qrcode";
import {getRegisterCode} from "@/api/system/user";
import defAva from '@/assets/images/profile.jpg'

export default {
  name: "Index",
  dicts: ['channel'],
  data() {
    return {
      // 版本号
      version: "3.9.0",
      imageUrl: defAva,
      userType: '',
      channel: '程琢',
    }
  },
  created() {
    this.userType = this.$store.getters.userType
    if (this.userType === '20') {
      this.$router.push({path: "/kaoshi/shouye"}).catch(() => {
      })
    } else {
      this.generateQRCode()
    }
  },
  methods: {
    generateQRCode() {
      getRegisterCode().then(res => {
        console.log(res.data)
        const opts = {
          errorCorrectionLevel: 'H', // 容错级别
          type: 'image/png', // 生成的二维码类型
          quality: 0.4, // 二维码质量
          margin: 4, // 二维码留白边距
          width: 250, // 宽
          height: 250, // 高
          text: 'https://system.hongcheng.vip/register?u=' + res.data + "&c=" + encodeURIComponent(this.channel), // 二维码内容
          color: {
            dark: '#333333', // 前景色
            light: '#fff'// 背景色
          }
        }
        this.QRCodeMsg = 'https://system.hongcheng.vip/register?u=' + res.data + "&c=" + encodeURIComponent(this.channel) // 生成的二维码为URL地址js
        // 将获取到的数据（val）画到msg（canvas）上
        const canvas = this.$refs.qrCanvas;
        QRCode.toDataURL(canvas, this.QRCodeMsg, opts, function (error) {
          console.log(error)
        })
        // const canvas = this.$refs.qrCanvas;
        // const ctx = canvas.getContext('2d');
        // const image = new Image();
        // image.src = this.imageUrl;
        // image.onload = () => {
        //   QRCode.toDataURL(canvas, this.QRCodeMsg, opts, function (error) {
        //     ctx.drawImage(image, 125, 125, 50, 50); // 将图片绘制到Canvas上。注意这里的尺寸要和二维码的实际尺寸相匹配。
        //
        //     console.log(error)
        //   })
        // }


      })


    },
  }
}
</script>

<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }

  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }

  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
  }

  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;

  ul {
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>

